package com.xujie.domain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xujie.domain.service.WeatherDailyService;
import com.xujie.infra.entity.WeatherDaily;
import com.xujie.infra.mapper.WeatherDailyMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class WeatherDailyServiceImpl implements WeatherDailyService {

  @Autowired
  private WeatherDailyMapper weatherDailyMapper;

  @Override
  public List<WeatherDaily> getLast7DaysWeather(String city) {
    // 计算7天前的日期
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_MONTH, -7);
    Date sevenDaysAgo = calendar.getTime();

    // 构建查询条件
    LambdaQueryWrapper<WeatherDaily> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(WeatherDaily::getCity, city)
        .ge(WeatherDaily::getDataDate, sevenDaysAgo)
        .orderByAsc(WeatherDaily::getDataDate);

    // 查询数据
    return weatherDailyMapper.selectList(queryWrapper);
  }
}